package com.spring.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.boot.config.security.MyPasswordEncoder;
import com.spring.boot.dao.AuthorityDao;
import com.spring.boot.dao.RoleDao;
import com.spring.boot.dao.UsersDao;
import com.spring.boot.entity.Authority;
import com.spring.boot.entity.Role;
import com.spring.boot.entity.Users;
import com.spring.boot.entity.server.PlatformUser;
import com.spring.boot.entity.server.ServerEntity;
import com.spring.boot.tool.Encode;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 自定义逻辑控制认证逻辑
 *
 * @author 刘骄阳
 * 2021-07-12 16:21
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    UsersDao usersDao;

    @Resource
    RoleDao roleDao;

    @Resource
    AuthorityDao authorityDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<Users> where = new QueryWrapper<>();
        where.eq("username", username);
        //从数据库,查询用户信息
        Users users = usersDao.selectOne(where);
        if (users == null) {
            throw new UsernameNotFoundException("此账号不存在!");
        }

        //通过用户id,获取角色
        Role role = roleDao.checkRole(users.getId());
        //通过角色id,获取权限
        String authority = authorityDao.checkAuthority(role.getId());
        //将角色和权限存入
        List<GrantedAuthority> list = AuthorityUtils.commaSeparatedStringToAuthorityList(authority);
        //将角色名存入List
        list.add(new SimpleGrantedAuthority(role.getName()));
        //创建对象
        PlatformUser platformUser = new PlatformUser(username, new MyPasswordEncoder().encode(users.getPassword()), list);
        //赋值id
        platformUser.setId(users.getId());
        //将权限的数据赋给users
        users.setAuthority(new ArrayList<>(Arrays.asList(authority.split(","))));
        //将角色名赋给users
        users.setRole(role.getName());
        //删除不需要的数据
        users.setFriends(null);
        users.setShield(null);
        users.setHearImg(null);
        users.setNickName(null);
        users.setPassword(null);
        //赋值最后登录的时间
        users.setLastLoginTime(new Date());
        //根据users,生成token
        platformUser.setToken(Encode.base64EncodeBySalt(ServerEntity.gson.toJson(users), 0));
        //打印,token
        System.out.println(platformUser.getToken());
        //返回
        return platformUser;
    }
}